#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <libxml/xmlreader.h>
#include <libtiffanygirl/libtiffanygirl.h>
#include "systray_menu.h"
#include "note.h"
#include "tiffanygirl.h"

extern TiffanyGirl *tiffgirl;

void note_read_text(TGirlNoteData *tgnotedata, GtkWidget *text)
{
	xmlTextReaderPtr reader;
	GtkTextBuffer *buffer;
	GtkTextIter iter;
	gchar *content;
	gboolean hastitle = FALSE;

	if (tgnotedata->text==NULL)
		return;

	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
	gtk_text_buffer_create_tag(buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL);
	gtk_text_buffer_create_tag(buffer, "italic", "style", PANGO_STYLE_ITALIC, NULL);
	gtk_text_buffer_create_tag(buffer, "title", "font", "Sans 16", NULL);

	/* insert text into buffer */
	reader = xmlReaderForMemory(tgnotedata->text, strlen(tgnotedata->text), NULL, NULL, 0);
	if (reader==NULL)
		return;

	gtk_text_buffer_get_start_iter(buffer, &iter);
	while(xmlTextReaderRead(reader)) {
//		printf("%s\n", xmlTextReaderConstName(reader));
		if (xmlStrcmp(xmlTextReaderConstName(reader), "bold")==0) {
			content = (gchar *)xmlTextReaderReadInnerXml(reader);
			gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, content, -1, "bold", NULL);
			xmlTextReaderRead(reader);
			xmlTextReaderRead(reader);
		} else if (xmlTextReaderConstName(reader)) {
			content = (gchar *)xmlTextReaderConstValue(reader);
			if (content==NULL)
				continue;

			if (hastitle) {
				gtk_text_buffer_insert(buffer, &iter, content, -1);
			} else {
				hastitle = TRUE;
				gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, tgnotedata->title, -1, "title", NULL);
				gtk_text_buffer_insert(buffer, &iter, content+strlen(tgnotedata->title), -1);
			}
		}
	}


	xmlFreeTextReader(reader);

	gtk_text_iter_set_offset(&iter, tgnotedata->cursor);
	gtk_text_buffer_place_cursor(buffer, &iter);
}

void note_window_destroy(GtkWidget *widget, TiffanyGirlNoteWindow *tgwin)
{
	GList *list;
	TiffanyGirlNote *tiffgnote;

	/* remove from window list */
	for (list=tiffgirl->windowlist;list;list=g_list_next(list)) {
		tiffgnote = (TiffanyGirlNote *)list->data;

		/* find the note which is opened already */
		if (tgwin->tgnote==tiffgnote->tgnote) {
			tiffgirl->windowlist = g_list_remove(tiffgirl->windowlist, tiffgnote);
			break;
		}
	}

	/* release */
	gtk_widget_destroy(tgwin->text);
	gtk_widget_destroy(tgwin->scrollwin);
	gtk_widget_destroy(tgwin->window);
	g_free(tgwin->tgnotedata->id);
	g_free(tgwin->tgnotedata->title);
	g_free(tgwin->tgnotedata->text);
	g_free(tgwin->tgnotedata);
	g_free(tgwin);
}

TiffanyGirlNoteWindow *note_dialog_new(TGirlNote *tgnote)
{
	TiffanyGirlNoteWindow *tgwin;

	tgwin = (TiffanyGirlNoteWindow *)g_new0(TiffanyGirlNoteWindow, 1);
	tgwin->tgnote = NULL;
	tgwin->tgnotedata = NULL;

	/* window */
	tgwin->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_resize(GTK_WINDOW(tgwin->window), 320, 240);
	gtk_container_set_border_width(GTK_CONTAINER(tgwin->window), 0);
	g_signal_connect(tgwin->window, "destroy", G_CALLBACK(note_window_destroy), tgwin);

	/* scroll */
	tgwin->scrollwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(tgwin->scrollwin), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_placement(GTK_SCROLLED_WINDOW(tgwin->scrollwin), GTK_CORNER_BOTTOM_LEFT);
	gtk_container_add(GTK_CONTAINER(tgwin->window), tgwin->scrollwin);

	/* text */
	tgwin->text = gtk_text_view_new();
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(tgwin->text), GTK_WRAP_WORD_CHAR);
	gtk_container_add(GTK_CONTAINER(tgwin->scrollwin), tgwin->text);

	/* loading note content */
	if (tgnote) {
		tgwin->tgnote = tgnote;
		tgwin->tgnotedata = tiffanygirl_note_read(tgnote->id);
		if (tgwin->tgnotedata) {
			gtk_window_move(GTK_WINDOW(tgwin->window), tgwin->tgnotedata->x, tgwin->tgnotedata->y);

			if (tgwin->tgnotedata->width&&tgwin->tgnotedata->height)
				gtk_window_resize(GTK_WINDOW(tgwin->window), tgwin->tgnotedata->width, tgwin->tgnotedata->height);

			gtk_window_set_title(GTK_WINDOW(tgwin->window), tgnote->title);
			note_read_text(tgwin->tgnotedata, tgwin->text);
		}
	}

	gtk_widget_show_all(tgwin->window);

	return tgwin;
}
